package com.jinxin.platform.cdsmsmgr.controller;

import com.jinxin.platform.base.api.annotation.APCOSApi;
import com.jinxin.platform.base.api.annotation.APCOSApiOperation;
import com.jinxin.platform.base.api.constant.APCOSApiConstans;
import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cdsmsmgr.pojo.domains.SmsTemplateInfo;
import com.jinxin.platform.cdsmsmgr.pojo.domains.SysUserVO;
import com.jinxin.platform.cdsmsmgr.pojo.entity.Constant;
import com.jinxin.platform.cdsmsmgr.pojo.entity.ResultEntity;
import com.jinxin.platform.cdsmsmgr.pojo.vo.SignInfo;
import com.jinxin.platform.cdsmsmgr.pojo.vo.TemplateVo;
import com.jinxin.platform.cdsmsmgr.rpc.SysUserRpc;
import com.jinxin.platform.cdsmsmgr.service.ISmsTemplateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2020-06-29
 * @Author: Lingnan
 **/
@APCOSApi(value = "模板管理接口", developers = {"那个男人"}, description = "模板管理接口")
@RestController
@RequestMapping("/cdsmsmgr/smsTemplate")
@Slf4j
public class SmsTemplateController {
    @Autowired
    private ISmsTemplateService templateService;
    @Autowired
    private SysUserRpc sysUserRpc;

    @APCOSApiOperation(value = "分页获取模板", developers = {"lingnan"}, description = "分页获取模板",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_WEB)
    @GetMapping("/list")
    public Map<String,Object> list(@RequestParam long page, @RequestParam long limit,
                                   @RequestParam(name = "name",required = false)String name,Integer type, Integer status,
                                   @RequestParam(name = "accountType") Integer accountType){
        SysUserVO currentUser = sysUserRpc.getCurrentUser();
        if (currentUser == null) {
            log.error("未登录--,smsTemplate list接口");
            return null;
        }
        HashMap param = new HashMap();
        param.put("page", page);
        param.put("limit", limit);
        param.put("name", name);
        param.put("type", type);
        param.put("status", status);
        param.put("accountType", accountType);
        String userAccount = currentUser.getUserAccount();
        if(!Constant.ACCOUNT_ADMIN.equals(userAccount)){
            param.put("applyUser", userAccount);
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("code",0);
        resultMap.put("msg","");
        resultMap.put("count", templateService.listPageCount(param));
        resultMap.put("data", templateService.listPage(param));
        return resultMap;
    }

    @APCOSApiOperation(value = "保存模板", developers = {"lingnan"}, description = "保存模板",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_WEB)
    @PostMapping("/saveInfo")
    public ResultEntity saveInfo(SmsTemplateInfo templateInfo){
        return templateService.saveOrUpdate(templateInfo);
    }

    @APCOSApiOperation(value = "删除模板", developers = {"lingnan"}, description = "删除模板",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_WEB)
    @GetMapping("/delete")
    public ResultEntity delete(String id) {
        return templateService.deleteById(id);
    }

    @APCOSApiOperation(value = "删除模板", developers = {"lingnan"}, description = "删除模板",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_WEB)
    @GetMapping("/deleteTemplate")
    public ResultEntity deleteTemplate(String code, String id) {
        return templateService.deleteByCode(code,id);
    }

    @APCOSApiOperation(value = "申请模板", developers = {"lingnan"}, description = "申请模板",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_WEB)
    @PostMapping("/appSmsTemplate")
    public ResultEntity appSmsTemplate(SmsTemplateInfo templateInfo){
        return templateService.appSmsTemplate(templateInfo);
    }

    @APCOSApiOperation(value = "同步模板状态", developers = {"lingnan"}, description = "同步模板状态",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_WEB)
    @PostMapping("/syncTemplateStatus")
    public ResultEntity syncTemplateStatus(){
        // 更新模板审核状态
        return templateService.updateStatus();
    }

    @APCOSApiOperation(value = "查询同步模板列表", developers = {"lingnan"}, description = "查询同步模板列表",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_WEB)
    @GetMapping("/querySmsTemplateList")
    public Map<String,Object> querySmsTemplateList() {
        JsonResult jsonResult = templateService.querySmsTemplateList();
        int code = 0,count = 0;
        String msg = "success";
        List<TemplateVo> templateInfoList = new ArrayList<>();
        if(jsonResult.getSuccess()){
            templateInfoList = (List<TemplateVo>) jsonResult.getData();
            count = templateInfoList.size();
        }else {
            code = jsonResult.getStatus();
            msg = jsonResult.getMsg();
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("code",code);
        resultMap.put("msg",msg);
        resultMap.put("count", count);
        resultMap.put("data", templateInfoList);
        return resultMap;
    }

    @APCOSApiOperation(value = "刷新签名状态", developers = {"lingnan"}, description = "刷新签名状态",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_WEB)
    @GetMapping("/freshStatus")
    public JsonResult freshStatus(String id) {
        return templateService.freshStatus(id);
    }

    @APCOSApiOperation(value = "选择的同步的模板", developers = {"lingnan"}, description = "选择的同步的模板",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_WEB)
    @PostMapping("/syncSmsTemp")
    public JsonResult syncSmsTemp(@RequestBody List<TemplateVo> templateInfoList){
        return templateService.syncSmsTemp(templateInfoList);
    }

}
